package com.caimiao.miao.interfaces;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.caimiao.miao.R;
import com.caimiao.miao.library.push.PushControl;
import com.umeng.message.PushAgent;

public abstract class BaseActivity<P extends IBasePresenter> extends AppCompatActivity implements IBaseActView<P> {
    public static final int REQUEST_CAMERA = 0x1001;
    public static final int REQUEST_ALBUM = 0x1002;

    protected abstract int getLayoutRes();

    protected abstract void initUI();

    protected abstract void initLogic();

    protected P mPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        hideBottomNavigation();
        if (getLayoutRes() != 0) setContentView(getLayoutRes());
        PushControl.getInstance().onAppStart(this);
        initUI();
        initLogic();
    }

    @Override
    protected void onResume() {
        super.onResume();
        hideBottomNavigation();//弹窗or键盘后隐藏底部导航栏
    }

    @Override
    protected void onDestroy() {
        if (mPresenter != null) {
            mPresenter.detach();
            mPresenter = null;
        }
        super.onDestroy();
    }

    @Override
    public void setPresenter(P presenter) {
        mPresenter = presenter;
        mPresenter.start(this);
    }

    @Override
    public void close(Bundle extra) {
        if (extra != null) {
            Intent intent = getIntent();
            intent.putExtra("DATA", extra);
            setResult(RESULT_OK, intent);
        }
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (keyCode == KeyEvent.KEYCODE_BACK) return true;
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                Log.e("KEY", "code = KEYCODE_BACK");
                break;
            case KeyEvent.KEYCODE_HOME:
                Log.e("KEY", "code = KEYCODE_HOME");
                break;
            case KeyEvent.KEYCODE_MENU:
                Log.e("KEY", "code = KEYCODE_MENU");
                break;
            default:
                Log.e("KEY", "code = " + keyCode);
                break;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void setTitle(int title) {
        setTitle(getString(title));
    }

    public void setTitle(String title) {
        TextView mTvTitle = findViewById(R.id.navTvTitle);
        if (mTvTitle != null) mTvTitle.setText(title);
    }

    public void onBack(View view) {
        finish();
    }

    public BaseActivity getActivity() {
        return this;
    }

    protected void hideBottomNavigation() {
        //隐藏虚拟按键，并且全屏
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            //for new api versions.
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;

            int uiOption2 = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;

            decorView.setSystemUiVisibility(uiOptions);
        }
    }

    protected void setDisableDropStatusBar() {

    }
}
